﻿using System;
using System.Collections.Generic;
using System.Text;
using Org.Eurekaa.DNJ.UI.WebControls;
using System.ComponentModel;
using System.Web.UI.Design;
using System.ComponentModel.Design;
using System.Web.UI;
using System.Security.Permissions;
using System.Web.UI.Design.WebControls;
using System.Web.UI.HtmlControls;

namespace Org.Eurekaa.DNJ.UI.Design.WebControls
{

    [SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)]
    public class DNJBoxControlDesigner : CompositeControlDesigner
    {
        public DNJDialog boxCtrl;

        public override void Initialize(IComponent component)
        {
            base.Initialize(component);
            boxCtrl = (DNJDialog)component;
        }

        public override String GetDesignTimeHtml(DesignerRegionCollection regions)
        {
            EditableDesignerRegion editableRegion =
                new EditableDesignerRegion(this,"box");
            regions.Add(editableRegion);

            string str = base.GetDesignTimeHtml();
            return base.GetDesignTimeHtml();
        }
        protected override void OnClick(DesignerRegionMouseEventArgs e)
        {
            base.UpdateDesignTimeHtml();
        }

        public override string GetEditableDesignerRegionContent(EditableDesignerRegion region)
        {
            IDesignerHost host = (IDesignerHost)Component.Site.GetService(typeof(IDesignerHost));


            ITemplate template = boxCtrl.Content;
            
            if (template != null)
                return ControlPersister.PersistTemplate(template, host);

            base.UpdateDesignTimeHtml();
            return String.Empty;
        }

        public override void SetEditableDesignerRegionContent(EditableDesignerRegion region, string content)
        {
            if (content == null)
                return;


            // Get a reference to the designer host
            IDesignerHost host = (IDesignerHost)Component.Site.GetService(typeof(IDesignerHost));
            if (host != null)
            {
                // Create a template from the content string
                ITemplate template = ControlParser.ParseTemplate(host, content);

                if (template != null)
                {
                    boxCtrl.Content = template;
                }
            }
            base.UpdateDesignTimeHtml();
        }
        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            if (boxCtrl.designRegion != null)
                boxCtrl.designRegion.Attributes[DesignerRegion.DesignerRegionAttributeName] = "box";
//            (boxCtrl.Controls[1] as HtmlGenericControl).Attributes[DesignerRegion.DesignerRegionAttributeName] = "box";
        }


    }
}
